﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC_Uthorization.API.Application.Command.MenuCommand;
using RBAC_Uthorization.API.Application.Command.RoleMenuCommand;
using RBAC_Uthorization.API.Application.Command.RoleMenuFunCommand;
using RBAC_Uthorization.API.DTO;
using RBAC_Uthorization.API.DTO.Request.Menu;
using RBAC_Uthorization.API.DTO.Request.RoleMenu;
using RBAC_Uthorization.Domain;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace RBAC_Uthorization.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class MenuController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<MenuController> _logger;

        public MenuController(IMediator mediator, ILogger<MenuController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }
        /// <summary>
        /// 新增菜单
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> menuCreate(MenuCreateDto dto)
        {
            try
            {
                var entity = await _mediator.Send(new MenuCreateCommand() {
                    dto = dto, CreateName = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value,
                    sys_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Jti)?.Value

                });
                return entity;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "新增菜单控制器异常");
                return new ApiResult<int>() { Code=ResultCode.Fail,Message= "新增菜单控制器异常" };
            }
        }
        /// <summary>
        /// 角色菜单功能添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]

        public async Task<ApiResult<int>> RoleMenu(CreateRoleMenuDto dto)
        {
            try
            {
                var entity = await _mediator.Send(new RoleMenuCreateCommand() 
                { dto = dto,
                    CreateName = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value,
                    sys_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Jti)?.Value
                });
       
                return entity;
            }
            catch (Exception ex )
            {
                _logger.LogError(ex, "角色菜单添加控制器报错");
                throw;
            }
        }
        /// <summary>
        /// 角色菜单功能修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> RoleMenuUpdate(RoleMenuFunUpdateDto dto)
        {
            try
            {
                var entity = await _mediator.Send(new RoleMenuFunUpdateCommand() { 
                    dto = dto,
                    UpdateName = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value });
                return entity;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "角色菜单修改控制器报错");
                throw;
            }
        }
        /// <summary>
        /// 菜单删除
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> menuDelete(int[] id)
        {
            try
            {
                var entity = await _mediator.Send(new MenuDeleteCommand() { Id = id, UpdateName = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value, sys_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Jti)?.Value });
                return entity;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "删除菜单控制器异常");
                return new ApiResult<int>() { Code = ResultCode.Fail, Message = "删除菜单控制器异常" };
            }
        }
        /// <summary>
        /// 菜单修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> menuUpdate(MenuUpdateDto dto)
        {
            try
            {
                var entity = await _mediator.Send(new MenuUpdateCommand() { 
                    dto = dto, 
                    UpdateName = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value,
                    sys_id = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Jti)?.Value });
                return entity;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "修改菜单控制器异常");
                return new ApiResult<int>() { Code = ResultCode.Fail, Message = "修改菜单控制器异常" };
            }
        }

    }
}
